package com.dongdongshop.consumer;

import com.dongdongshop.constant.SecKillGoodsConstant;
import com.dongdongshop.mapper.TbSeckillGoodsMapper;
import com.dongdongshop.mapper.TbSeckillOrderMapper;
import com.dongdongshop.pojo.TbSeckillOrder;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RocketMQMessageListener(topic = "secOrder", consumerGroup = "ddshop-secKillOrder")
public class SecKillOrderConsumer implements RocketMQListener<TbSeckillOrder> {

    @Resource
    private TbSeckillOrderMapper tbSeckillOrderMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(TbSeckillOrder tbSeckillOrder) {
        tbSeckillOrderMapper.insertSelective(tbSeckillOrder);

        redisTemplate.boundHashOps(SecKillGoodsConstant.SEC_KILL_ORDER).put(tbSeckillOrder.getUserId(),tbSeckillOrder.getId());
    }
}
